package org.javaboy.demo;

import okhttp3.*;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author Lyh
 * @date 2024/2/20 1:21
 * @desciption:
 */
public class OkHttpDemo {
    public static void main(String[] args) throws InterruptedException {
        // 通过建造者模式去创建OkHttpClient对象
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .readTimeout(5, TimeUnit.SECONDS)
                .build();
        // 构建一个具体的请求
        Request request = new Request.Builder()
                .get()
                .url("http://www.javaboy.org")
                .build();
        Call call = client.newCall(request);
        CountDownLatch countDownLatch = new CountDownLatch(1);
        // 执行网络请求，处理请求结果
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                // 减一
                countDownLatch.countDown();
            }

            /**
             * 这个是请求成功的回调函数
             * @param call
             * @param response
             * @throws IOException
             */
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                countDownLatch.countDown();
                System.out.println("response.body().string() = " + response.body().string());
            }
        });
        // 会判断计数器是否为0，如果为0，才会继续执行后续的代码，否则就暂停在这里
        countDownLatch.await();
        // Thread.sleep(5000);笨方法停止5秒等回调异步执行，如果不等待则等不到回调结果
    }
}
